package com.zhang.sdk.utils.log;

import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.util.Log;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;


public class SaveLogStrategy {
        @NonNull
        private final Handler handler;

        public SaveLogStrategy(@NonNull Handler handler) {
            this.handler = (Handler) checkNotNull(handler);
        }

        public void log(int level, @Nullable String tag, @NonNull String message) {
            checkNotNull(message);
            handler.sendMessage(handler.obtainMessage(level, message));
        }


        public static Object checkNotNull(Object obj) {
            if (obj == null) {
                throw new NullPointerException();
            }
            return obj;
        }
        static class WriteHandler extends Handler {

            private final String folder;
            private final int maxFileSize;

            WriteHandler(@NonNull Looper looper, @NonNull String folder, int maxFileSize) {
                super((Looper) checkNotNull(looper));
                this.folder = (String) checkNotNull(folder);
                this.maxFileSize = maxFileSize;
            }

            @SuppressWarnings("checkstyle:emptyblock")
            @Override
            public void handleMessage(@NonNull Message msg) {
                String content = (String) msg.obj;
                FileWriter fileWriter = null;
                File logFile = getLogFile(folder, "logs");

                try {
                    fileWriter = new FileWriter(logFile, true);

                    writeLog(fileWriter, content);

                    fileWriter.flush();
                    fileWriter.close();
                } catch (IOException e) {
                    if (fileWriter != null) {
                        try {
                            fileWriter.flush();
                            fileWriter.close();
                        } catch (IOException e1) {

                        }
                    }
                }
            }

            private void writeLog(@NonNull FileWriter fileWriter, @NonNull String content) throws IOException {
                checkNotNull(fileWriter);
                checkNotNull(content);
                fileWriter.append("\n").append(content);
            }

            private File getLogFile(@NonNull String folderName, @NonNull String fileName) {
                checkNotNull(folderName);
                checkNotNull(fileName);

                File folder = new File(folderName);
                if (!folder.exists()) {
                    if (!folder.mkdirs()) {
                        Log.println(Log.ERROR, "saveLog", "文件未创建成功，可能是读写权限没给");
                    }
                }

                int newFileCount = 0;
                File newFile;
                File existingFile = null;

                newFile = new File(folder, String.format("%s_%s.txt", fileName, newFileCount));
                while (newFile.exists()) {
                    existingFile = newFile;
                    newFileCount++;
                    newFile = new File(folder, String.format("%s_%s.txt", fileName, newFileCount));
                }

                if (existingFile != null) {
                    if (existingFile.length() >= maxFileSize) {
                        return newFile;
                    }
                    return existingFile;
                }

                return newFile;
            }
        }
    }
